package com.huiquan.vocab.controller;

import com.huiquan.analysis.domain.User;
import com.huiquan.dyad.service.DyadInitService;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.vocab.dao.VocabDyadPositionDao;
import com.huiquan.vocab.service.VocabDyadInitService;
import com.huiquan.vocab.service.VocabDyadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class VocabDyadController {

	@Autowired
	private VocabDyadInitService vocabDyadInitService;
	@Autowired
	private VocabDyadService vocabDyadService;
	@Autowired
	private VocabDyadPositionDao vocabDyadPositionDao;
	@Autowired
	private DyadInitService dyadInitService;
	
	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_init.do")
	public ReturnData init(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		return vocabDyadInitService.init();
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/dyad_std_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			if (tabid.equals("24_2")) {
				pageParam.put("flagKey", "-1");
			} else if (tabid.equals("24_3")) {
				pageParam.put("flagKey", "0");
			} else if (tabid.equals("24_4")) {
				pageParam.put("flagKey", "1");
			}
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("typeKey", "p+p");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		// 获取页面参数，如果request中有值则修改session，否则从session中拿值
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String typeKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "typeKey");
		String flagKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "flagKey");
		String oWord = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "oWord");
		String oKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "oKey");
		String sWord = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "sWord");
		String sKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "sKey");
		String keysRelation = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keysRelation");
		String countPerPage = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String disPlayFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "disPlayFlag");
		String replaceFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "replaceFlag");
		String mergeFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "mergeFlag");
		String labelFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "labelFlag");
		String arrayKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "arrayKey");
		String arrayKey2 = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "arrayKey2");

		return vocabDyadService.list(start, typeKey, exactSearchFlag, flagKey, countPerPage, keysRelation, disPlayFlag,
				replaceFlag, mergeFlag, labelFlag, oWord, oKey, sWord, sKey, arrayKey, arrayKey2);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/dyad_std_intern_list.htm")
	public ModelAndView internList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String tabid = req.getParameter("tabid");
		User user = (User) req.getSession().getAttribute("user");

		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("flagKey", "-1");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		// 获取页面参数，如果request中有值则修改session，否则从session中拿值
		String flagKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "flagKey");
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String arrayKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "arrayKey");
		String internType = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "internType");

		return vocabDyadService.internList(flagKey, user, resp, req, start, arrayKey, internType);
	}

	@ResponseBody
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/dyad_std_check_page.do")
	public ReturnData checkPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		String type = (String) pageParam.get("typeKey");

		String checkedIds = req.getParameter("checkedIds");
		String unCheckedIds = req.getParameter("unCheckedIds");
		User user = (User) req.getSession().getAttribute("user");
		ReturnData rtn = null;
		try {
			rtn = vocabDyadService.checkPage(checkedIds, unCheckedIds, type, user);
		} catch (Exception e) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc(e.getMessage());
			return ReturnUtil.fail(rc);
		}

		return rtn;
	}

	@ResponseBody
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/dyad_std_check_page_test.do")
	public ReturnData checkPageTest(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		String checkedIds = req.getParameter("checkedIds");
		String unCheckedIds = req.getParameter("unCheckedIds");
		User user = (User) req.getSession().getAttribute("user");

		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		String type = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "internType");

		return vocabDyadService.checkPageTest(checkedIds, unCheckedIds, type, user);
	}

	@ResponseBody
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/dyad_change_std.do")
	public ReturnData changeStd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String dyad = req.getParameter("dyad");
		Boolean isTest = Boolean.parseBoolean(req.getParameter("isTest"));
		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		String type = (String) pageParam.get("typeKey");
		User user = (User) req.getSession().getAttribute("user");

		return vocabDyadService.changeStd(id, dyad, user, type, isTest);
	}

	@ResponseBody
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/dyad_delete_synonym.do")
	public ReturnData deleteSynonym(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String dyad = req.getParameter("dyad");
		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		String type = (String) pageParam.get("typeKey");
		User user = (User) req.getSession().getAttribute("user");

		return vocabDyadService.deleteSynonym(dyad, type, user);
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_std_merge.do")
	public ReturnData stdMerge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String ids = req.getParameter("ids");
		User user = (User) req.getSession().getAttribute("user");
		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		String type = (String) pageParam.get("typeKey");
		return vocabDyadService.stdMerge(ids, user,type);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_std_add_remove.do")
	public ReturnData stdAddRemove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String id = req.getParameter("id");
		String resultArray = req.getParameter("resultArray");
		String resultPattern = req.getParameter("resultPattern");
		String confirmFlagStr = req.getParameter("confirmFlag");
		boolean confirmFlag = confirmFlagStr != null && "1".equals(confirmFlagStr);

		return vocabDyadService.stdAddRemove(id, resultArray, resultPattern, confirmFlag);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_std_pre_flag.do")
	public ReturnData stdPreFlag(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String preFlag = req.getParameter("preFlag");
		Boolean isTest = Boolean.parseBoolean(req.getParameter("isTest"));
		User user = (User) req.getSession().getAttribute("user");
        String type = req.getParameter("type");
		return vocabDyadService.stdPreFlag(id, preFlag, user, isTest,type);
	}

	@ResponseBody
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/dyad_std_submit_base.do")
	public ReturnData stdSubmitBase(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");

		// 在参数列表中获取
		String type = req.getParameter("typeKey");
		// 如果没有就在session中获取
		if (type == null) {
			type = (String) pageParam.get("typeKey");
		}
		// 初始化所有二元组
		dyadInitService.init();

		return ReturnUtil.success();
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_std_label_flag.do")
	public ReturnData stdLabelFlag(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String labelFlag = req.getParameter("labelFlag");
		User user = (User) req.getSession().getAttribute("user");

		return vocabDyadService.stdLabelFlag(id, labelFlag, user);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_std_merge_flag.do")
	public ReturnData stdMergeFlag(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String mergeFlag = req.getParameter("mergeFlag");
		User user = (User) req.getSession().getAttribute("user");

		return vocabDyadService.stdMergeFlag(id, mergeFlag, user);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_std_display_flag.do")
	public ReturnData stdDisplayFlag(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String displayFlag = req.getParameter("displayFlag");
		User user = (User) req.getSession().getAttribute("user");

		return vocabDyadService.stdDisplayFlag(id, displayFlag, user);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_std_replace_flag.do")
	public ReturnData stdReplaceFlag(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String replaceFlag = req.getParameter("replaceFlag");
		User user = (User) req.getSession().getAttribute("user");

		return vocabDyadService.stdReplaceFlag(id, replaceFlag, user);
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_change_synonym.do")
	public ReturnData changeSynonmy(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String synonym = req.getParameter("synonym");
		Map<String, Object> pageParam;
		pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		String typeKey = (String) RequestParamUtil.getParamByRequestOrSession(req, pageParam, "typeKey");
		return vocabDyadService.changeSynonmy(id, synonym, typeKey);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_std_update_combine_s.do")
	public ReturnData stdUpdateCombineS(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String combineS = req.getParameter("combineS");
		User user = (User) req.getSession().getAttribute("user");
		String type = req.getParameter("type");

		if ((combineS.endsWith("0,") || combineS.indexOf("1,0") > -1) && "o+o".equals(type)) {
			return ReturnUtil.fail(ReturnCode.EXCEPTION);
		}
		return vocabDyadService.stdUpdateCombineS(id, combineS, user);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_std_update_sentence_order.do")
	public ReturnData stdUpdateSentenceOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String sentenceOrder = req.getParameter("sentenceOrder");
		User user = (User) req.getSession().getAttribute("user");

		return vocabDyadService.stdUpdateSentenceOrder(id, sentenceOrder, user);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/dyad_o_de_update_flag.do")
	public ReturnData updateODeFlag(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		User user = (User) req.getSession().getAttribute("user");

		return vocabDyadService.updateODeFlag(id,user);
	}
	@ResponseBody
	@RequestMapping(value = "/vocab/merge_true_dyad_by_element_std.do")
	public ReturnData mergeTrueDyadByElementStd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		vocabDyadService.mergeTrueDyadByElementStd();
		
		vocabDyadPositionDao.init();
		
		return ReturnUtil.success();
	}
}
